package com.wnhz.work.yz.fragment.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AddTaskActivity;
import com.wnhz.work.yz.activity.TaskAll2Activity;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.TaskBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/3/11.
 */
public class Task2Fragment4 extends BaseFragment {

    private View view;
    private RecyclerView recyclerView;
    private TaskAll2Activity context;
    private String user_id;

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public void setContext(TaskAll2Activity context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_plan_item, container, false);
        return view;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        upLoad();
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onResume() {
        upLoad();
        super.onResume();
    }

    private List<TaskBean.InfoEntity> infos = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        map.put("user_id", user_id);
        map.put("status", 3);
        context.showDialog();
        XUtil.Post(Url.TASK_GETTASKLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                infos.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        TaskBean bean = gson.fromJson(result, TaskBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                            context.MyToast(info);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                context.closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                context.closeDialog();
            }
        });
    }

    private void setData() {
        recyclerView.setAdapter(new BaseRVAdapter(context, infos) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_task_fragment;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.head_img), infos.get(position).getUser_img());
                holder.setTextView(R.id.tv_name, infos.get(position).getRealname());
                holder.setTextView(R.id.tv_msg, infos.get(position).getTask_content());
                holder.setTextView(R.id.tv_time, infos.get(position).getCreate_at());
                holder.setTextView(R.id.tv_start_time, "开始：" + infos.get(position).getStart_time());
                holder.setTextView(R.id.tv_end_time, "截止" + infos.get(position).getEnd_time());
                holder.getView(R.id.tv_finish).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finishTask(0, infos.get(position).getTask_id());
                    }
                });
                holder.getView(R.id.tv_detry).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finishTask(1, infos.get(position).getTask_id());
                    }
                }); holder.getView(R.id.max_ll).setVisibility(View.GONE);
                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(context, AddTaskActivity.class)
                                .putExtra("task_id", infos.get(position).getTask_id())
                                .putExtra("type", type)
                        );
                    }
                });
            }

        });
    }

    private void finishTask(int i, String task_id) {
        String url = "";
        if (i == 0) {
            url = Url.TASK_COMPLETETASK;
        } else {
            url = Url.TASK_ENDTASK;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("task_id", task_id);
        context.showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                infos.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        context.MyToast(info);
                        upLoad();  } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        context.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                context.closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                context.closeDialog();
            }
        });


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        upLoad();
    }


    private void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

    }

}
